<template>
  <div class="login-register">
    <div class="contain">
      <div class="big-box" :class="{ active: isLogin }">
        <div class="big-contain" key="bigContainLogin" v-if="isLogin">
          <div class="btitle">账户登录</div>
          <div class="bform">
            <input placeholder="账号" v-model="loginInfo.username" />
            <span class="errTips" v-if="idError">* 账号填写错误 *</span>
            <input
              type="password"
              placeholder="密码"
              v-model="loginInfo.password"
            />
            <span class="errTips" v-if="passwordError">* 密码填写错误 *</span>
          </div>

          <el-radio-group v-model="loginInfo.identity">
            <el-radio :label="0">学生</el-radio>
            <el-radio :label="1">教师</el-radio>
            <el-radio :label="2">管理员</el-radio>
          </el-radio-group>
          <br />
          <div class="login-button">
            <button class="bbutton" @click="login">登录</button>
            <div @click="toTopicSquare">游客入口</div>
          </div>
        </div>
      </div>
      <div class="small-box" :class="{ active: isLogin }">
        <div class="small-contain" key="smallContainRegister" v-if="isLogin">
          <div class="stitle">你好，工大人!</div>
          <p class="scontent">一起来讨论吧！</p>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import loginApi from "@/api/loginApi";
import personApi from "@/api/personApi";
export default {
  name: "login-register",
  data() {
    return {
      isLogin: true,
      idError: false,
      passwordError: false,
      existed: false,
      loginInfo: {
        username: "",
        password: "",
        identity: "",
      },
    };
  },
  methods: {
    login() {
      var username = this.loginInfo.username;
      var password = this.loginInfo.password;
      var identity = this.loginInfo.identity;
      if (identity == 0) {
        identity = "student";
      } else if (identity == 1) {
        identity = "teacher";
      } else {
        identity = "admin";
      }
      this.isVerify(username, password, identity);
    },
    isVerify(username, password, identity) {
      //首先校验是否可以登录,若不可，则直接返回
      loginApi.isVerify(username, password, identity).then((response) => {
        if (response.code == 200) {
          sessionStorage.clear();
          var token = response.data;
          sessionStorage.setItem("isLogin", 1);
          sessionStorage.setItem("token", token);
          sessionStorage.setItem("username", username);
          var identity = this.loginInfo.identity;
          if (identity == 0 || identity == 1) {
            sessionStorage.setItem("identity", identity);
          } else if (identity == 2) {
            sessionStorage.setItem("isAdmin", 1);
          }
          //已经校验成功后会执行下面代码
          personApi.getPersonInfo().then((response) => {
            if (response.code == 200) {
              let info = response.data[0];
              sessionStorage.setItem("isAdmin", response.data[0].isadmin);
              sessionStorage.setItem("isfirsttimelogin", info.isfirsttimelogin);
              //第一次登录时，需要先跳转到兴趣选择页面,教师表和学生表的参数不一样！！！
              if (info.isfirsttimelogin == 1 || info.isFirstTimeLogin == 1) {
                this.$router.push({
                  name: "interestChoice",
                });
              }
              //如果是不是第一次登录，就跳转到首页
              else {
                setTimeout(() => {
                  this.$router.push({
                    path: "/",
                    params: {
                      isLogin: 1,
                    },
                  });

                  location.reload();
                }, 250);
              }
            }
          });
        }
        //说明校验失败
        else {
          return;
        }
      });
    },
    getPersonInfo() {
      personApi.getPersonInfo().then((response) => {
        console.log(response.data[0]);
        return response.data[0];
      });
    },
    toTopicSquare() {
      this.$router.push({
        path: "/",
      });
    },
  },
};
</script>

<style scoped="scoped">
.login-register {
  width: 100vw;
  height: 100vh;
  box-sizing: border-box;
}
.contain {
  width: 60%;
  height: 60%;
  position: relative;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #fff;
  border-radius: 20px;
  box-shadow: 0 0 3px #f0f0f0, 0 0 6px #f0f0f0;
}
.big-box {
  width: 70%;
  height: 100%;
  position: absolute;
  top: 0;
  left: 30%;
  transform: translateX(0%);
  transition: all 1s;
}
.big-contain {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}
.btitle {
  font-size: 1.5em;
  font-weight: bold;
  color: rgb(57, 167, 176);
}
.bform {
  width: 100%;
  height: 40%;
  padding: 2em 0;
  display: flex;
  flex-direction: column;
  justify-content: space-around;
  align-items: center;
}
.bform .errTips {
  display: block;
  width: 50%;
  text-align: left;
  color: red;
  font-size: 0.7em;
  margin-left: 1em;
}
.bform input {
  width: 50%;
  height: 30px;
  border: none;
  outline: none;
  border-radius: 10px;
  padding-left: 2em;
  background-color: #f0f0f0;
}
.login-button {
  position: relative;
}
.login-button div {
  position: absolute;
  top: 50%;
  right: -100%;
  text-decoration: underline;
  color: rgba(0, 0, 0, 0.5);
}
.bbutton {
  width: 100px;
  height: 40px;
  border-radius: 24px;
  border: none;
  outline: none;
  background-color: rgb(57, 167, 176);
  color: #fff;
  font-size: 0.9em;
  cursor: pointer;
}
.small-box {
  width: 30%;
  height: 100%;
  background: linear-gradient(135deg, rgb(57, 167, 176), rgb(56, 183, 145));
  position: absolute;
  top: 0;
  left: 0;
  transform: translateX(0%);
  transition: all 1s;
  border-top-left-radius: inherit;
  border-bottom-left-radius: inherit;
}
.small-contain {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}
.stitle {
  font-size: 1.5em;
  font-weight: bold;
  color: #fff;
}
.scontent {
  font-size: 0.8em;
  color: #fff;
  text-align: center;
  padding: 2em 4em;
  line-height: 1.7em;
}
.sbutton {
  width: 60%;
  height: 40px;
  border-radius: 24px;
  border: 1px solid #fff;
  outline: none;
  background-color: transparent;
  color: #fff;
  font-size: 0.9em;
  cursor: pointer;
}

.big-box.active {
  left: 0;
  transition: all 0.5s;
}
.small-box.active {
  left: 100%;
  border-top-left-radius: 0;
  border-bottom-left-radius: 0;
  border-top-right-radius: inherit;
  border-bottom-right-radius: inherit;
  transform: translateX(-100%);
  transition: all 1s;
}
</style>
